<?php
namespace Home\Controller;
use Think\Controller;

class SolrController extends BaseController {

	/**
	 * 搜索结果
	 * @param string $keyword 关键字
	 * @param string $page 页码
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function index(){
		$keyword = I('get.keyword', '720p', "rawurlencode");
		$keyword = str_replace("q=", "", $keyword);
		//$keyword = mb_substr($keyword, 0, 192); // 限制keyword长度，防止抛出Data/Name too long错误
		//$keyword = str_replace("-", " ", $keyword);
		$page = I('get.page', 1);
		$perPage = 10;
		$start = ($page-1) * $perPage;
		// base url
		//$baseUrl = '/' . strtolower(CONTROLLER_NAME) . '/' . urlencode($keyword);
		$baseUrl = '/list/' . urlencode($keyword);

		$url = "http://www.codeit.cn:8080/solr/codeit/select?q=all%3A{$keyword}&start={$start}&rows=10&wt=json&indent=true&hl=true&hl.fl=title%2Cfile&hl.simple.pre=%3Cem+class%3D%22highlight%22%3E&hl.simple.post=%3C%2Fem%3E&f.file.hl.fragsize=350&_=1415549063557";

		$json = file_get_contents($url);

		$data = json_decode($json, true);

		$list = $data['response']['docs'];

		foreach ($data['response']['docs'] as $dk => $dv) {
			foreach ($data['highlighting'] as $hk => $hv) {
				if ($dv['hash'] == $hk and !empty($hv['title'])) {
					$list[$dk]['hl_title'] = $hv['title'];
					$list[$dk]['htl_file'] = $hv['file'];
				} else if (empty($hv['title'])) {
					$list[$dk]['hl_title'] = $list[$dk]['title'];
					$list[$dk]['htl_file'] = $list[$dk]['file'];
				}
			}
		}

		// gen pager
		if ($data['response']['numFound'] > $perPage) {
			$pagerBegin = max($page - 5, 1);
			$pagerEnd = min($pagerBegin + 10, ceil($data['response']['numFound'] / $perPage) + 1);
			$pager = '';
			do {
				$pager .= ($pagerBegin == $page) ? "		<li class=\"disabled\"><a>{$page}</a></li>\r\n" : "		<li><a href=\"{$baseUrl}/{$pagerBegin}\">{$pagerBegin}</a></li>\r\n";
			} while (++$pagerBegin < $pagerEnd);
		}

		// display
		$this->assign('keyword', rawurldecode($keyword));
		$this->assign('hot', $hot);
		$this->assign('list', $list);
		$this->assign('cost', number_format($data['responseHeader']['QTime'], 2));
		$this->assign('count', $data['response']['numFound']);
		$this->assign('pager', $pager);
		$this->display();
			
	}

	/**
	 * 热门关键词
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function hotWords() {
		try {
			$xs = new \XS('codeit');
			$search = $xs->search;
			$search->setCharset('UTF-8');

			$hot = $search->getHotQuery(50);
			//var_dump($hot);
			$this->assign("list", $hot);
			$this->display();
		} catch (XSException $e) {
				
		}
	}

	/**
	 * 精选
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function best() {
		//模型
		$model = D('Best');
		$this->show("<h1>精选内容</h1>");
	}

	/**
	 * 教程
	 * @access public
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function howto() {
		//模型
		$model = D('Howto');
		$this->show("<h1>教程</h1>");
	}

	/**
	 * 随机展示20条
	 * @access public
	 * @since 2014-10-20 20:37:13
	 * @author FrankKung <Frank@kofj.net>
	 */
	public function explorer() {
		//模型
		$Hash = D('Hash');

		// 随机数据
		$redis= new \Redis();
		$redis->connect('115.28.215.209',6379);
		$randMember = $redis->sRandMember('maindb',20);
		//var_dump($randMember);
		
		$map['hash'] = array('in', $randMember);
		$members = $Hash->where($map)->field('hash,title,file,fileNumber,fileSize,hot')->select();
		$this->assign("list", $members);
		//var_dump($members);

		$this->display();
	}
}